package com.xyzk.diagnose.view.dialog;

import com.xyzk.diagnose.R;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public  class BaseDialog extends Dialog{
	private Activity context;
	private TextView tvTitle;
	private Button btnSure, btnCancel;
	private EditText etContent;
	private TextView spinner;
	private TextView tvContent;
	private int resId;

	public BaseDialog(Activity context) {
		super(context);
		this.context = context;
	}

	public BaseDialog(Activity context, int resId) {
		super(context);
		this.context = context;
		this.resId = resId;
	}

	/**
	 * 设置dialog样式
	 */
	public View initView(){
		View view = View.inflate(context, R.layout.dialog_show,null);
		tvTitle = (TextView)view.findViewById(R.id.tv_title);
		btnSure = (Button) view.findViewById(R.id.btn_dialog_sure);
		btnCancel = (Button) view.findViewById(R.id.btn_dialog_cancel);

		etContent = (EditText)view.findViewById(R.id.et_dialog_input);
		spinner = (TextView) view.findViewById(R.id.tv_dialog_select);
		tvContent = (TextView) view.findViewById(R.id.tv_dialog_show);
		View layout = setLayout();
		showDialog(view);
		return layout;
	}

	/**
	 * 设置对应的布局
	 * @return 返回对应的控件
	 */
	private View setLayout(){
		switch (resId) {
			case R.id.et_dialog_input:
				etContent.setVisibility(View.VISIBLE);
				spinner.setVisibility(View.GONE);
				tvContent.setVisibility(View.GONE);
				return etContent;
			case R.id.tv_dialog_select:
				etContent.setVisibility(View.GONE);
				spinner.setVisibility(View.VISIBLE);
				tvContent.setVisibility(View.GONE);
				return spinner;
			case R.id.tv_dialog_show:
				etContent.setVisibility(View.GONE);
				spinner.setVisibility(View.GONE);
				tvContent.setVisibility(View.VISIBLE);
				return tvContent;
		}
		return null;
	}

	/**
	 * 设置显示dialog样式
	 */
	public void showDialog(View view){
		super.show();
		Window window = getWindow();
		window.setBackgroundDrawable(new ColorDrawable(0x00000000));
		window.setContentView(view);
		WindowManager.LayoutParams wl = window.getAttributes();
		DisplayMetrics metric  = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(metric);;
		wl.width = (int) (metric.widthPixels * 0.58);
		window.setAttributes(wl);
		//点击dialog外部不消失
		this.setCanceledOnTouchOutside(false);
	}


	/**
	 * 设置标题
	 * @param text
	 */
	public void setTitle(String text){
		tvTitle.setText(text);
	}

	/**
	 * 设置标题
	 * @param resId 资源
	 */
	public void setTitle(int resId){
		tvTitle.setText(context.getString(resId));
	}

	/**
	 * 获取确定button
	 * @return button
	 */
	public Button getButtonSure(){
		return btnSure;
	}

	/**
	 * 获取取消button
	 * @return button
	 */
	public Button getButtonCancel(){
		return btnCancel;
	}

	/**
	 * 隐藏取消控件
	 */
	public void hideCancelButton(){
		btnCancel.setVisibility(View.GONE);
	}

	/**
	 * 显示取消控件
	 */
	public void showCancelButton(){
		btnCancel.setVisibility(View.VISIBLE);
	}

	/**
	 * 回调处理
	 * @param l
	 */
	public void onClickListener(android.view.View.OnClickListener click){
		btnSure.setOnClickListener(click);
	};

}
